package com.yourname.myapplication.presentation.screen.quiz

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun QuizScreen(viewModel: QuizViewModel) {
    // 1. 明确指定泛型类型
    val questions by viewModel.questions.observeAsState(emptyList())
    val currentIndex by viewModel.currentIndex.observeAsState(0)
    val score by viewModel.score.observeAsState(0)
    // 2. 安全访问当前问题
    val currentQuestion = questions.getOrNull(currentIndex)

    Column(modifier = Modifier.padding(16.dp)) {
        // 3. 确保Text组件参数明确
        Text(
            text = "分数: $score",
            style = MaterialTheme.typography.titleMedium
        )

        currentQuestion?.let { question ->
            // 4. 明确text属性存在
            Text(
                text = question.text, // 确保Question类有text属性
                style = MaterialTheme.typography.bodyLarge
            )

            Spacer(modifier = Modifier.height(16.dp))

            // 5. 明确options的类型
            question.options.forEachIndexed { index: Int, option: String ->
                Button(
                    onClick = { viewModel.checkAnswer(index) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 4.dp)
                ) {
                    Text(text = option)
                }
            }
        } ?: Text(text = "加载中...")
    }
}